package com.ffcs.crmd.tsp.api.dto.page;

import java.io.Serializable;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAliasType;

@XStreamAliasType("PageInfo")
public class PageInfo<T> implements Serializable {
    
    public PageInfo(){
        
    }
    
    public PageInfo(List<T> list) {
        this.list = list;
    }
    
    private static final long serialVersionUID = 1L;
    
    public static final int DEFAULT_PAGE_SIZE = 20;
    
    // 当前页
    private int             pageNum;
    // 每页的数量
    private int             pageSize;
    // 当前页的数量
    private int             size;
    // 由于startRow和endRow不常用，这里说个具体的用法
    // 可以在页面中"显示startRow到endRow 共size条数据"
    
    // 当前页面第一个元素在数据库中的行号
    private int             startRow;
    // 当前页面最后一个元素在数据库中的行号
    private int             endRow;
    // 总记录数
    private long            total;
    // 总页数
    private int             pages;
    // 结果集
    private List<T>         list;
    
    // 第一页
    private int             firstPage;
    // 前一页
    private int             prePage;
    // 下一页
    private int             nextPage;
    // 最后一页
    private int             lastPage;
    
    // 是否为第一页
    private boolean         isFirstPages      = false;
    // 是否为最后一页
    private boolean         isLastPages       = false;
    // 是否有前一页
    private boolean         hasPreviousPages  = false;
    // 是否有下一页
    private boolean         hasNextPages      = false;
    // 导航页码数
    private int             navigatePages;
    // 所有导航页号
    private int[]           navigatePageNums;
    
    
    /**
     * 
     * 功能说明:计算前后页，第一页，最后一页
     *
     */
    private void calcPage() {
        if (navigatePageNums != null && navigatePageNums.length > 0) {
            firstPage = navigatePageNums[0];
            lastPage = navigatePageNums[navigatePageNums.length - 1];
            if (pageNum > 1) {
                prePage = pageNum - 1;
            }
            if (pageNum < pages) {
                nextPage = pageNum + 1;
            }
        }
    }
    
    /**
     * 
     * 功能说明:判定页面边界
     *
     */
    private void judgePageBoudary() {
        isFirstPages = pageNum == 1;
        isLastPages = pageNum == pages && pageNum != 1;
        hasPreviousPages = pageNum > 1;
        hasNextPages = pageNum < pages;
    }
    
    
    /**
     * 功能说明: 计算相关的分页结果参数
     * 
     * @param pageSize
     * @param pageNumber
     * @param totalCounts
     */
    public void calcAndJudge(int pageSize, int pageNumber, int totalCounts, List<T> dataList) {
        int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
        int totalPages = totalCounts / pageSize;
        if ((totalCounts % pageSize) > 0) {
            totalPages += 1;
        }
        this.setTotal(totalCounts);
        this.setPages(totalPages);
        this.setNavigatePages(totalPages);
        this.setPageNum(pageNumber);
        this.setPageSize(pageSize);
        
        if (dataList.size() == 0) {
            this.setStartRow(0);
            this.setEndRow(0);
        } else {
            this.setStartRow(first + 1);
            this.setEndRow(first + dataList.size());
        }
        calcPage();
        judgePageBoudary();
    }
    
    
    /**
     * 
     * 功能说明:计算实际分页开头行数
     * 
     * @param pageSize 分页大小
     * @param pageNumber 页数
     * @return
     */
    public static int getPageBeginSize(int pageSize, int pageNumber){
        if (pageSize == 0 || pageSize < 0) {
            pageSize = PageInfo.DEFAULT_PAGE_SIZE;
        }
        
        if (pageNumber == 0 || pageNumber < 0) {
            pageNumber = 1;
        }
        return (pageNumber - 1) * pageSize;
    }
    
    /**
     * 
     * 功能说明:计算实际分页结尾行数
     * 
     * @param first
     * @param pageSize
     * @param totalCounts
     * @return
     */
    public static int getPageEndSize(int first, int pageSize, int totalCounts){
        if (pageSize == 0) {
            pageSize = PageInfo.DEFAULT_PAGE_SIZE;
        }
        
        return first + pageSize > totalCounts ? totalCounts : first + pageSize;
    }
    
    
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
    
    public int getPageNum() {
        return pageNum;
    }
    
    public int getPageSize() {
        return pageSize;
    }
    
    public int getSize() {
        return size;
    }
    
    public int getStartRow() {
        return startRow;
    }
    
    public int getEndRow() {
        return endRow;
    }
    
    public long getTotal() {
        return total;
    }
    
    public int getPages() {
        return pages;
    }
    
    public List<T> getList() {
        return list;
    }
    
    public int getFirstPage() {
        return firstPage;
    }
    
    public int getPrePage() {
        return prePage;
    }
    
    public int getNextPage() {
        return nextPage;
    }
    
    public int getLastPage() {
        return lastPage;
    }
    
    public boolean isFirstPages() {
        return isFirstPages;
    }
    
    public boolean isLastPages() {
        return isLastPages;
    }
    
    public boolean isHasPreviousPages() {
        return hasPreviousPages;
    }
    
    public boolean isHasNextPages() {
        return hasNextPages;
    }
    
    public int getNavigatePages() {
        return navigatePages;
    }
    
    public int[] getNavigatePageNums() {
        return navigatePageNums;
    }
    
    public void setList(List<T> retList) {
        this.list = retList;
    }
    
    public void setEndRow(int last) {
        this.endRow = last;
    }
    
    public void setPages(int totalPages) {
        this.pages = totalPages;
    }
    
    public void setTotal(long totalCounts) {
        this.total = totalCounts;
    }
    
    public void setStartRow(int first) {
        this.startRow = first;
    }
    
    public void setPageSize(int pageSize2) {
        this.pageSize = pageSize2;
    }
    
    public void setNavigatePages(int navigatePages) {
        this.navigatePages = navigatePages;
    }
    
    public void setSize(int size) {
        this.size = size;
    }
    
    public void setFirstPage(int firstPage) {
        this.firstPage = firstPage;
    }
    
    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }
    
    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }
    
    public void setLastPage(int lastPage) {
        this.lastPage = lastPage;
    }
    
    public void setFirstPages(boolean isFirstPages) {
        this.isFirstPages = isFirstPages;
    }
    
    public void setLastPages(boolean isLastPages) {
        this.isLastPages = isLastPages;
    }
    
    public void setHasPreviousPages(boolean hasPreviousPages) {
        this.hasPreviousPages = hasPreviousPages;
    }
    
    public void setHasNextPages(boolean hasNextPages) {
        this.hasNextPages = hasNextPages;
    }
    
    public void setNavigatePageNums(int[] navigatePageNums) {
        this.navigatePageNums = navigatePageNums;
    }
    
    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("PageInfo{");
        sb.append("pageNum=").append(pageNum);
        sb.append(", pageSize=").append(pageSize);
        sb.append(", size=").append(size);
        sb.append(", startRow=").append(startRow);
        sb.append(", endRow=").append(endRow);
        sb.append(", total=").append(total);
        sb.append(", pages=").append(pages);
        sb.append(", list=").append(list);
        sb.append(", firstPage=").append(firstPage);
        sb.append(", prePage=").append(prePage);
        sb.append(", nextPage=").append(nextPage);
        sb.append(", lastPage=").append(lastPage);
        sb.append(", isFirstPage=").append(isFirstPages);
        sb.append(", isLastPage=").append(isLastPages);
        sb.append(", hasPreviousPage=").append(hasPreviousPages);
        sb.append(", hasNextPage=").append(hasNextPages);
        sb.append(", navigatePages=").append(navigatePages);
        sb.append(", navigatepageNums=");
        if (navigatePageNums == null)
            sb.append("null");
        else {
            sb.append('[');
            for (int i = 0; i < navigatePageNums.length; ++i)
                sb.append(i == 0 ? ""
                    : ", ").append(navigatePageNums[i]);
            sb.append(']');
        }
        sb.append('}');
        return sb.toString();
    }


}
